package com.color.admin.common.core.page;

import java.util.List;

/**
 * @author HaiQiu
 * @date 2021/11/18
 * 分页响应工具
 */
public class PageResponse<T> extends Page {

    /**
     * 分页返回结果数据
     */
    private List<T> data;

    private Long totalElements;

    private Long pages;

    public PageResponse() {
        super();
    }

    public PageResponse(List<T> data) {
        super();
        this.data = data;
    }

    public PageResponse(int pageIndex, int pageSize) {
        super(pageIndex, pageSize);
    }

    public PageResponse(int pageIndex, int pageSize, List<T> data) {
        super(pageIndex, pageSize);
        this.data = data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public List<T> getData() {
        return data;
    }

    public Long getTotalElements() {
        if (totalElements == null || totalElements < 0) {
            return this.totalElements = 0L;
        } else {
            return this.totalElements;

        }

    }

    public void setTotalElements(Long totalElements) {
        this.totalElements = totalElements;
        calculatePages();
    }

    public Long getPages() {
        return pages;
    }

    public void calculatePages() {
        Long totalElements = getTotalElements();
        if (totalElements % pageSize == 0) {
            this.pages = totalElements / pageSize;
        } else {
            this.pages = totalElements / pageSize + 1;
        }
    }

    @Override
    public void setPageSize(int pageSize) {
        super.setPageSize(pageSize);
        calculatePages();
    }
}
